package com.workmanager.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.JobTaskBean;
import com.workmanager.bean.StudentBean;
import com.workmanager.bean.WorkBean;
import com.workmanager.service.LookUpGoodJobService;
import org.aspectj.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by haocongshun on 2016/9/8.
 */
@RequestMapping("/LookUpGoodJob")
@Controller("LookUpGoodJob")
public class LookUpGoodJobController {
    @Resource(name="lookUpGoodJobService")
    private LookUpGoodJobService lookUpGoodJobService;
    public LookUpGoodJobService getLookUpGoodJobService() {
        return lookUpGoodJobService;
    }

    public void setLookUpGoodJobService(LookUpGoodJobService lookUpGoodJobService) {
        this.lookUpGoodJobService = lookUpGoodJobService;
    }
    @RequestMapping("/goodJob")
    public void getGoodJob(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jobTask_id = request.getParameter("jobTask_id");
        JSONObject json = new JSONObject();
        List<WorkBean> listGoodJob = getLookUpGoodJobService().getGoodJob(jobTask_id);
        json.put("goodJob",listGoodJob);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/jobTask")
    public void getJobTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        StudentBean stu = (StudentBean) request.getSession().getAttribute("学生");
        List<JobTaskBean> listJobTask = lookUpGoodJobService.getJobTask(stu.getStudent_id());
        json.put("jobTask",listJobTask);
        System.out.println(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/downloadGoodJob")
    public void downloadGoodJob(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String work_id = request.getParameter("id");
        WorkBean work = lookUpGoodJobService.getWorkById(work_id);
        String path = work.getWork_file();
        String realname = work.getWork_title();
        path = request.getSession().getServletContext().getRealPath(path);
        File file = new File(path);
        if(!file.exists()){
            System.out.println("资源已被删除");
            return;
        }
        //设置响应头，控制浏览器下载该文件
        response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
        //读取要下载的文件，保存到文件输入流
        FileInputStream in = new FileInputStream(path);
        //创建输出流
        OutputStream out = response.getOutputStream();
        //创建缓冲区
        FileUtil.copyStream(in,out);
        //关闭文件输入流
        in.close();
        //关闭输出流
        out.close();
    }

}
